pub struct Constant {}

impl Constant {
    pub const COLOR_MAP: [(u32, &'static str); 9] = [
        (0x00bb88, "GREEN"),
        (0x0af3ff, "BLUE"),
        (0x000000, "BLACK"),
        (0xff0000, "RED"),
        (0x333333, "GRAY"),
        (0xffffff, "WHITE"),
        (0xff1493, "DeepPink"),
        (0x191970, "MidnightBlue"),
        (0xffff00, "Yellow"),
    ];

    pub fn color_hex(color_number: u32) -> String {
        let color_str = format!("{:x}", color_number);
        format!("#{}", color_str)
    }

    pub fn color_hex_number(color: &str) -> u32 {
        let re = regex::Regex::new(r"^#([0-9a-fA-F]{3}|[0-9a-fA-F]{6})$").unwrap();
        let mut result = 0x000000;
        if re.is_match(color) {
            let color_slice = &color[1..];
            result = u32::from_str_radix(color_slice, 16).unwrap();
        }
        result
    }

    pub fn color_rgb(color_i: &str) -> String {
        let re = regex::Regex::new(r"^#([0-9a-fA-F]{3}|[0-9a-fA-F]{6})$").unwrap();
        let mut color = color_i.to_lowercase();
        if re.is_match(&color) {
            if color.len() == 4 {
                let mut color_new = String::from("#");
                for i in 1..color.len() {
                    color_new.push_str(&color[i..=i]);
                    color_new.push_str(&color[i..=i]);
                }
                color = color_new;
            }
            let mut color_change = Vec::new();
            for i in (1..color.len()).step_by(2) {
                color_change.push(u8::from_str_radix(&color[i..=i + 1], 16).unwrap());
            }
            format!(
                "RGB({})",
                color_change
                    .iter()
                    .map(|c| c.to_string())
                    .collect::<Vec<String>>()
                    .join(",")
            )
        } else {
            String::from(color)
        }
    }

    pub fn color_random() -> u32 {
        rand::random::<u32>() % 16777215
    }
}
